﻿$(document).ready(function () {

    //Show sticky help
    /*if ($.getQueryStringValue('mode') == 'new')
        $.showHelpMessage('You can only rename the photo once it has not been uploaded|Photo name will be used as the caption if Default Caption(s) is ticked|Remember maximum size is 5MB/photo|Remember to give the Album a name|Read the "hints" on the form too');
    else if ($.getQueryStringValue('mode') == 'edit')
        $.showHelpMessage('Edit albums has not yet been implemented');*/
    //startCreatingAlbum();
    // Animate the "button"
    $('.optionButton').mouseenter(function () {
        $(this).stop(true, true).animate(
        {
            width: '310',
            color: '#fff'
        },
        {
            duration: 'slow',
            easing: 'easeOutQuad'
        });
    });

    $('.optionButton').mouseleave(function () {
        $(this).stop(true, true).animate(
        {
            width: '160',
            color: '#FFF17C'
        },
        {
            duration: 'fast',
            easing: 'easeOutQuad'
        });
    });

    //Hide Create button at the begining
    $('#btnCreateNewAlbum').css('display', 'none');

    //Handle button click
    $('#create_button').click(function () {
        window.location = 'ManageAlbum.aspx?mode=new';
    });
    $('#edit_button').click(function () {
        window.location = 'ManageAlbum.aspx?mode=edit';
    });
    $('#btnCreateNewAlbum').click(function () {
        //Title validation
        if ($('#txtCreateAlbumName').val().trim() == '') {
            $('#txtCreateAlbumName').requiredModeOn();
            $.showGrowlMessage(GrowlType.ERROR, 'Album Name is required.');
            return false;
        }
        //Hide/block the UI for processing
        startCreatingAlbum();
    });

    //"Un-required" the title box on focus
    $('#txtCreateAlbumName').focus(function () {
        $('#txtCreateAlbumName').requiredModeOff();
    });

    $("#uploader").plupload({
        // General settings
        runtimes: 'silverlight',
        url: 'plupload.ashx',
        max_file_size: '5mb',
        chunk_size: '50kb',
        unique_names: false,
        multiple_queues: true,

        // Resize images on clientside if we can
        //resize: { width: 320, height: 240, quality: 90 },

        // Rename files by clicking on their titles
        rename: true,

        // Sort files
        sortable: true,

        // Specify what files to browse for
        filters: [
			{ title: "Image files", extensions: "jpg,gif,png" }
		],

        // Silverlight settings
        silverlight_xap_url: 'plupload.silverlight.xap'
    });

    //Instance of the uploader
    var uploader = $('#uploader').plupload('getUploader');

    //Bind file add event
    uploader.bind('FilesAdded', OnUploaderFilesAdded);
    //Bind state change event
    uploader.bind('StateChanged', OnUploaderStateChanged);
    //Bind error handler
    uploader.bind('Error', OnUploaderError);

    //Create clear queue link
    $('.plupload_filelist_header .plupload_file_action').html('Clear').click(function () {
        //Clear the queue
        var uploader = $('#uploader').plupload('getUploader');
        uploader.splice(0, uploader.files.length);
        $.showGrowlMessage(GrowlType.INFO, 'The photo(s) queue is cleared');
    }).mouseenter(function () {
        $(this).css('color', '#fff');
    }).mouseleave(function () {
        $(this).css('color', '#FFF17C');
    });
});

/********************************** Helpers *************************************/

//Show error
function OnUploaderError(uploader, error) {
    $.showGrowlMessage(GrowlType.ERROR, error.message);
}

//Uploading state changed, show/hide Create button
function OnUploaderStateChanged(uploader) {
    if (uploader.state == plupload.STARTED) {
        $('#btnCreateNewAlbum').fadeOut();
    }
    else if (uploader.state == plupload.STOPPED) {
        //Show message and enable Create button
        $.showGrowlMessage(GrowlType.INFO, 'Upload finished! You can now process creating the Album!');
        //Remove page message
        $('#pageMessage').html('');
        $('#btnCreateNewAlbum').fadeIn();
    }
}

//Not allowing add new files to the queue while uploading
function OnUploaderFilesAdded(uploader, files) {
    if (uploader.state == plupload.STARTED) {
        $.showGrowlMessage(GrowlType.WARNING, 'Please wait until the current upload finishes!');
        for (var i = 0; i < files.length; i++) {
            uploader.removeFile(files[i]);
        }
    }
}

//Hide all edit controls and show loading panel
function startCreatingAlbum() {
    $('#create_new_album').showLoading();
    $('#create_new_album_content').css('display', 'none')
    $('#pageMessage').html('Creating the album. Please wait...').css('color', '#fff');
}